import request from "./request";
import qs from "qs";

export function get(url, data = {}) {
  return request({
    url: url,
    method: "get",
    transformRequest: [
      function(data) {
        return qs.stringify(data);
      }
    ],
    data: data
  });
}

export function post(url, data = {}) {
  return request({
    url: url,
    method: "post",
    transformRequest: [
      function(data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
        // return qs.stringify(data);
      }
    ],
    data: data
  });
}

export function put(url, data = {}) {
  return request({
    url: url,
    method: "put",
    transformRequest: [
      function(data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
        // return qs.stringify(data);
      }
    ],
    data: data
  });
}

export function del(url) {
  return request({
    url: url,
    method: "delete"
  });
}

export function uploadFile(url, params) {
  return request({
    url: url,
    data: params,
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
}
